SWEP.PrintName = "Sonic Screwdriver"
SWEP.Author = "RononDex"
SWEP.Purpose = ""
SWEP.Instructions = "Left Click to Use, Right Click to Change Setting"
SWEP.Category = "Doctor Who"
SWEP.Spawnable=true
SWEP.AdminSpawnable=false
SWEP.Base = "weapon_base"
SWEP.Slot = 1
SWEP.SlotPos = 3
SWEP.DrawAmmo	= false
SWEP.DrawCrosshair = true
--SWEP.HoldType = "knife"
SWEP.ViewModelFOV = 55
SWEP.ViewModelFlip = false
SWEP.ViewModel = "models/2k5sonicview.mdl"
SWEP.WorldModel = "models/2k5sonicview.mdl"

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo	= "none"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo	= "none"

util.PrecacheSound("RononDex/sonic.wav")

function SWEP:Initialize()
 
	self.SMode = 1
	self.NextUse = CurTime()
	self:SetNWInt("Mode",1)

end

if CLIENT then

function SWEP:Lights()

	local pos = self:GetPos()+self:GetUp()*25+self:GetForward()*20+self:GetRight()*15
	
	local dynlight = DynamicLight(self:EntIndex() + 4096);
	dynlight.Pos = pos
	dynlight.Brightness = 6;
	dynlight.Size = 34;
	dynlight.Decay = 1024;
	dynlight.R = 124;
	dynlight.G = 205;
	dynlight.B = 245;
	dynlight.DieTime = CurTime()+1;
	
end

function SWEP:Think()

	if self.Owner:KeyDown(IN_ATTACK) then
		self:Lights()
	end
end

function SWEP:DrawHUD()

	local m = self:GetNWInt("Mode",1)
	local th = self:GetNWInt("TargetHealth",0)
	
	if th < 1 then
		th = "N/A"
	else
		th = self:GetNWInt("TargetHealth",0)
	end
	
	if m == 1 then
		self.strSMode = "(Open/Close Doors)"
	elseif m == 2 then
		self.strSMode = "(Lock Doors/Consoles)"		
	elseif m == 3 then
		self.strSMode = "(Unlock Doors/Consoles)"
	elseif m == 4 then
		self.strSMode = "(Call TARDIS)"
	elseif m == 5 then
		self.strSMode = "(Health Scan)"
		draw.WordBox(9,ScrW()-218,ScrH()-80,"Target Health: "..th,"Default",Color(0,0,0,80),Color(255,220,0,220));
	elseif m == 6 then
		self.strSMode = "(Disarm/Disable)"
	elseif m == 7 then
		self.strSMode = "(Heal Minor Wounds)"
	elseif m == 8 then
		self.strSMode = "(Ignite)"
	elseif m == 9 then
		self.strSMode = "(High Freq. Sound)"
	end
	
	draw.WordBox(9,ScrW()-240,ScrH()-115,"Mode: "..m..(math.random(1000,4000)).."Hz "..self.strSMode,"Default",Color(0,0,0,80),Color(255,220,0,220));

end

end

function SWEP:GetSMode()
	
	local m = self.SMode
	
	if m == 1 then
		self.strSMode = "(Open/Close Doors)"
	elseif m == 2 then
		self.strSMode = "(Lock Doors/Consoles)"		
	elseif m == 3 then
		self.strSMode = "(Unlock Doors/Consoles)"
	elseif m == 4 then
		self.strSMode = "(Call TARDIS)"
	elseif m == 5 then
		self.strSMode = "(Health Scan)"
	elseif m == 6 then
		self.strSMode = "(Disarm/Disable)"
	elseif m == 7 then
		self.strSMode = "(Heal Minor Wounds)"
	elseif m == 8 then
		self.strSMode = "(Ignite)"
	elseif m == 9 then
		self.strSMode = "(High Freq. Sound)"
	end
end
	
function SWEP:PrimaryAttack()

	local m = self.SMode
	local snd = "RononDex/sonic.wav"

	if m == 1 then		
		self:OpenDoors()
		self:EmitSound(snd,100,100)		
	elseif m == 2 then
		self:LockDoors()
		self:EmitSound(snd,100,110)
	elseif m == 3 then
		self:UnlockDoors()
		self:EmitSound(snd,100,120)
	elseif m == 4 then
		self:CallTardis()
		self:EmitSound(snd,100,130)
	elseif m == 5 then
		self:CheckVitals()
		self:EmitSound(snd,100,140)
	elseif m == 6 then
		self:Disarm()
		self:EmitSound(snd,100,150)
	elseif m == 7 then
		self:HealMinorWounds()
		self:EmitSound(snd,100,160)
	elseif m == 8 then
		self:SetFire()
		self:EmitSound(snd,100,175)
	elseif m == 9 then
		self:BreakGlass()
		self:EmitSound(snd,100,185)
	end
end

function SWEP:SecondaryAttack()
	
	if self.SMode == 9 then
		self.SMode = 1
		self:SetNWInt("Mode",self.SMode)
		return
	end
	self.SMode = self.SMode + 1	
	self:SetNWInt("Mode",self.SMode)
end

function SWEP:OpenDoors()
		
	local tr = self.Owner:GetEyeTrace()
	local ent = tr.Entity
	local pos = self.Owner:GetPos()	
	
	if ValidEntity(ent) then
		local dist = (pos - ent:GetPos()):Length()
		if ent:GetClass()=="prop_door_rotating" then
			if dist < 500 then				
				ent:Input("Toggle")
			end
		elseif ent:GetClass()=="func_button" then
			if dist < 300 then
				if self.NextUse < CurTime() then
					ent:Input("Press")
					self.NextUse = CurTime() + 1
				end
			end
		end
	end
end

function SWEP:UnlockDoors()
		
	local tr = self.Owner:GetEyeTrace()
	local ent = tr.Entity
	local pos = self.Owner:GetPos()	
	
	if ValidEntity(ent) then
		local dist = (pos - ent:GetPos()):Length()
		if ent:GetClass()=="prop_door_rotating" then
			if dist < 500 then				
				ent:Input("Unlock")
			end
		elseif ent:GetClass()=="func_button" then
			if dist < 300 then
				ent:Input("Unlock")
			end
		elseif ent:GetClass()=="tardis" then
			if dist < 500 then
				ent:LockTardis(false)
			end
		elseif ent:GetClass()=="tardis_smith" then
			if dist < 500 then
				ent:LockTardis(false)
			end			
		end
	end
end

function SWEP:LockDoors()
		
	local tr = self.Owner:GetEyeTrace()
	local ent = tr.Entity
	local pos = self.Owner:GetPos()	
	
	if ValidEntity(ent) then
		local dist = (pos - ent:GetPos()):Length()
		if ent:GetClass()=="prop_door_rotating" then
			if dist < 500 then				
				ent:Input("Lock")
			end
		elseif ent:GetClass()=="func_button" then
			if dist < 500 then	
				ent:Input("Lock")
			end
		elseif ent:GetClass()=="tardis" then
			if dist < 500 then
				ent:LockTardis(true)
			end
		elseif ent:GetClass()=="tardis_smith" then
			if dist < 500 then
				ent:LockTardis(true)
			end			
		end
	end
end

function SWEP:CallTardis()
	
	local tr = self.Owner:GetEyeTrace()
	
	for _,v in pairs(ents.FindByClass("tardis")) do
		if(ValidEntity(v)) then
			if(v.Owner==self.Owner) then
				v.Remote = true
				v.Target = tr.HitPos
				v:Shift()
			end
		end
	end
	
	for _,v in pairs(ents.FindByClass("tardis_smith")) do
		if(ValidEntity(v)) then
			if(v.Owner==self.Owner) then
				v.Remote = true
				v.Target = tr.HitPos
				v:Shift()
			end
		end
	end	
end

function SWEP:CheckVitals()

	local tr  = self.Owner:GetEyeTrace()
	local e = tr.Entity
	
	if ValidEntity(e) then
		if e:IsNPC() then			
			self:SetNWInt("TargetHealth",e:Health())
		elseif e:IsPlayer() then			
			self:SetNWInt("TargetHealth",e:Health())
		end
		timer.Simple(3, function() self:SetNWInt("TargetHealth",0) end)
	end
end

function SWEP:Disarm()

	local tr = self.Owner:GetEyeTrace()
	local e = tr.Entity
	
	if(ValidEntity(e)) then
		if(e:IsNPC()) then
			if(e:GetClass()=="npc_cscanner") then
				e:TakeDamage(2)
			elseif(e:GetClass()=="npc_manhack") then
				e:TakeDamage(2)
			elseif(e:GetClass()=="npc_turret_floor") then
				e:Input("SelfDestruct")
			elseif(e:GetClass()=="npc_rollermine") then
				e:Input("InteractivePowerDown")
			elseif(e:GetClass()=="npc_dog") then
				e:Input("SetHealth",5)
				e:Input("Ignite")
			end
		elseif(e:IsPlayer()) then
			e:DropWeapon(e:GetActiveWeapon())
		elseif(e:GetClass()=="gmod_rtcameraprop") then
			e:TakePhysicsDamage(100)
		end
	end
end

function SWEP:BreakGlass()
	
	for _,v in pairs(ents.FindInSphere(self.Owner:GetPos(),450)) do
		if ValidEntity(v) then
			if v:GetClass()=="func_breakable_surf" then
				v:Input("Break")				
			elseif v:GetClass()=="func_breakable" then
				v:Input("Break")
			elseif v:GetClass()=="prop_physics" then
				if v:GetModel()=="models/props_phx/oildrum001_explosive.mdl" then
					v:Input("Ignite")
				elseif v:GetModel()=="models/props_phx/misc/flakshell_big.mdl" then
					v:Input("Ignite")
				elseif v:GetModel()=="models/props_phx/ww2bomb.mdl" then
					v:Input("Ignite")
				elseif v:GetModel()=="models/props_junk/gascan001a.mdl" then
					v:Input("Ignite")
				elseif v:GetModel()=="models/props_c17/oildrum001_explosive.mdl" then
					v:Input("Ignite")
				end
			elseif v:GetClass()=="gmod_balloon" then
				v:TakeDamage(10)
			end
			if(v.Inputs) then
				for k,e in pairs(v.Inputs) do				
					v:TriggerInput(k,math.random(1,500))
				end
			end
		end
	end
end

function SWEP:HealMinorWounds()

	local tr = self.Owner:GetEyeTrace()
	local e = tr.Entity
	local pos = self.Owner:GetPos()
	
	if ValidEntity(e) then
		local dist = (pos - e:GetPos()):Length()
		if dist < 500 then
			if e:IsPlayer() then
				if e:Health() > 50 and e:Health() < 100 then
					e:SetHealth(e:Health()+1)
				end
			elseif e:IsNPC() then
				if e:Health() > 50 and e:Health() < 70 then
					e:SetHealth(e:Health()+1)
				end				
			end
		end
	end
end

function SWEP:SetFire()

	local tr = self.Owner:GetEyeTrace()
	local pos = self.Owner:GetPos()
	local dist = (pos - tr.HitPos):Length()
	
	if tr.Entity:IsPlayer() then return end
	if tr.Entity:IsNPC() then return end

	if dist < 500 then
		tr.Entity:Ignite(5)
	end
end
